import java.util.ArrayList;
import java.util.Calendar;

import services.interfaces.BillingServiceAdapter;
import services.interfaces.ServicesFactory;
import use_case.handlereturns.Storage;
import use_case.processRental.Customer;
import use_case.processRental.Sale;
import junit.framework.TestCase;

public class BillingServiceAdapterTest extends TestCase {
	private Storage inventory;
	private Customer customer1, customer2;
	private Sale sale1, sale2;
	private BillingServiceAdapter billingService;
	private ArrayList<Customer> customer;
	private ArrayList<Sale> sale;

	public BillingServiceAdapterTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
		inventory = new Storage();
		inventory.initialize();
		customer1 = new Customer("Tamer", "1231516460");
		customer2 = new Customer("Roger", "1230646151");
		sale1 = new Sale(inventory);
		sale2 = new Sale(inventory);
		customer = new ArrayList<Customer>();
		sale = new ArrayList<Sale>();
		billingService = (BillingServiceAdapter) ServicesFactory.getInstance().getBillingServiceAdapter();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		inventory = null;
		customer1 = null;
		customer2 = null;
		sale1 = null;
		sale2 = null;
		billingService = null;
		customer = null;
		sale = null;
	}

	public void testAddRentalRecord() {
		Calendar returnDay = Calendar.getInstance();
		sale1.setReturnDate(returnDay);
		
		int expected = billingService.getNumberOfRentalRecords() + 1;
		billingService.addRentalRecord(customer1, sale1);
		assertTrue(billingService.getNumberOfRentalRecords() == expected);
	}
	
	public void testUpdateRentalRecord() {
		customer.add(customer2);
		sale.add(sale2);
		
		billingService.updateRentalRecord(customer, sale);
		assertTrue(billingService.getNumberOfRentalRecords() == customer.size());
	}

	public void testRemoveRentalRecord() {
		Calendar returnDay = Calendar.getInstance();
		sale1.setReturnDate(returnDay);
		billingService.addRentalRecord(customer1, sale1);
		
		int expected = 0;
		if (billingService.getNumberOfRentalRecords() > 0) {
			expected = billingService.getNumberOfRentalRecords() - 1;
		}
		billingService.removeRentalRecord(0);
		assertTrue(billingService.getNumberOfRentalRecords() == expected);
	}

	public void testOverdueCharge() {
		Calendar dueDay = Calendar.getInstance();
		dueDay.roll(Calendar.DATE, false);
		sale1.setDueDate(dueDay);
		sale2.setDueDate(dueDay);
		
		Calendar returnDay = Calendar.getInstance();
		returnDay.roll(Calendar.DATE, false);
		sale1.setReturnDate(returnDay);
		
		billingService.addRentalRecord(customer1, sale1);
		billingService.addRentalRecord(customer2, sale2);
		billingService.overdueCharge();
	}

	public void testIsOverdue() {
		Calendar dueDay = Calendar.getInstance();
		dueDay.roll(Calendar.DATE, false);
		assertTrue(billingService.isOverdue(dueDay));
	}
	
	public void testIsNotOverdue() {
		Calendar dueDay = Calendar.getInstance();
		assertTrue(billingService.isOverdue(dueDay) == false);
	}

}
